#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"

void display()
{
	printf("=======================================\n");
	printf("******BuiDer's AddressBook System******\n");
	printf("=======================================\n");
	printf("*******1.add               2.del*******\n");
	printf("*******3.search            4.modify****\n");
	printf("*******5.show              6.sort******\n");
	printf("*************** 0.exit ****************\n");
	printf("=======================================\n");
}

int check_capacity(AddressBook* p)  //support:InitialAddressBook -- check the capacity is enough or not. If not, increase the capacity
{
	if (p->size == p->capacity)
	{
		PeoInfo* temp = (PeoInfo*)realloc(p->data, (p->capacity + INC_SIZE) * sizeof(PeoInfo));
		if (temp == NULL)
		{
			perror("check_capacity:realloc");
			return 0;
		}
		else
		{
			p->data = temp;
			p->capacity += INC_SIZE;
			return 1;
		}
	}
}

int LoadData(AddressBook* p)  //support:InitialAddressBook -- Load Data form BIN document;
{
	FILE* pf = fopen("data.dat", "rb");
	if (pf == NULL)
	{
		perror("LoadData:fopen");
		return 1;
	}

	PeoInfo temp = { 0 };
	while (fread(&temp, sizeof(PeoInfo), 1, pf))
	{
		check_capacity(p);

		p->data[p->size] = temp;
		p->size++;
	}

	fclose(pf);
	pf = NULL;

	return;
}

int InitialAddressBook(AddressBook* p)
{
	p->data = (PeoInfo*)malloc(sizeof(PeoInfo) * DEFAULT_CAPACITY);
	if (p->data == NULL)
	{
		perror("Initial:malloc");
		return 1;
	}
	p->size = 0;
	p->capacity = 0;

	LoadData(p);

	return;
}

void addInfo(AddressBook* p)
{
	check_capacity(p);

	printf("Please input a name for the person you wanna add:>\n");
	scanf("%s", p->data[p->size].name);
	printf("Please input the age of this person:>\n");
	scanf("%d", &(p->data[p->size].age));
	printf("Please input the gender of this person:>\n");
	scanf("%s", p->data[p->size].gender);
	printf("Please input the phone number of this person:>\n");
	scanf("%s", p->data[p->size].number);

	p->size++;

	printf("Added Sucessfully!\n");
	Sleep(500);
}

int FindByName(AddressBook* p, char name[])   //support:searchInfo & delInfo -- find information by name,is existed, return location; if not, return -1
{
	for (int i = 0; i < p->size; i++)
	{
		if (!strcmp(p->data[i].name, name))
		{
			return i;
			break;
		}
	}

	return -1;
}

void searchInfo(AddressBook* p)
{
	char* Tname[10];
	printf("Please input the name you wanna serach:>\n");
	scanf("%s", Tname);

	int i = FindByName(p, Tname);

	if (i != -1)
	{
		printf("\n\n================================================\n");
		printf("%-10s\t%-4s\t%-5s\t%-12s\n", "Name", "Age", "Gender", "Phone Number");
		printf("================================================\n");
		printf("%-10s\t%-4d\t%-5s\t%-12s\n\n\n\n", p->data[i].name, p->data[i].age, 
			p->data[i].gender, p->data[i].number);

		printf("Press 'Enter' to continue:>");
		getchar();
		getchar();

		return;
	}
	else
	{
		printf("The person is not exsited in this address book.\n");
		Sleep(1000);
	}

	printf("Not Founded :(\n");
}

void delInfo(AddressBook* p)
{
	if (p->size == 0)
	{
		printf("The address book is empty!\n");
		Sleep(500);
		return;
	}

	char* Tname[10];
	printf("Please input the name you wanna serach:>\n");
	scanf("%s", Tname);

	int i = FindByName(p, Tname);

	if (i != -1)
	{
		for (int j = i; j < p->size; j++)
		{
			p->data[j] = p->data[j++];
		}
		p->size--;
	}
	else
	{
		printf("The person is not exsited in this address book.\n");
		Sleep(1000);
	}
}

void modifyInfo(AddressBook* p)
{
	char* Tname[10];
	printf("Please input the name you wanna modify:>\n");
	scanf("%s", Tname);

	int i = FindByName(p, Tname);

	if (i != -1)
	{
		printf("Please input a new name:>\n");
		scanf("%s", p->data[i].name);
		printf("Please input a new age:>\n");
		scanf("%d", &(p->data[i].age));
		printf("Please input a new gender:>\n");
		scanf("%s", p->data[i].gender);
		printf("Please input a new phone number:>\n");
		scanf("%s", p->data[i].number);

		printf("Modified Sucessfully!\n");
		Sleep(500);

		return;
	}
	else
	{
		printf("The person is not exsited in this address book.\n");
		Sleep(1000);
	}
}

void showInfo(AddressBook* p)
{
	printf("\n\n================================================\n");
	printf("%-10s\t%-4s\t%-5s\t%-12s\n", "Name", "Age", "Gender", "Phone Number");
	printf("================================================\n");
	for (int i = 0; i < p->size; i++)
	{
		printf("%-10s\t%-4d\t%-5s\t%-12s\n\n\n\n", p->data[i].name, p->data[i].age, 
			p->data[i].gender, p->data[i].number);
	}

	printf("Press 'Enter' to continue:>");
	getchar();
	getchar();
}

int compareByAge(const void* a, const void* b)
{
	PeoInfo* p1 = (PeoInfo*)a;
	PeoInfo* p2 = (PeoInfo*)b;

	return p2->age - p1->age;
}

void sortByAgeInfo(AddressBook* p)
{
	qsort(p->data, p->size, sizeof(PeoInfo), compareByAge);

	printf("Sorted Sucessfully!\n");

	Sleep(500);
}

void saveData(AddressBook* p)
{
	FILE* pf = fopen("data.dat", "wb");
	if (pf == NULL)
	{

		perror("saveData:fopen");
		return 1;
	}
	
	for (int i = 0; i < p->size; i++)
	{
		fwrite(p->data + i, sizeof(PeoInfo), 1, pf);
	}

	fclose(pf);
	pf = NULL;
}

void freeMemory(AddressBook* p)
{
	free(p->data);
	p->data = NULL;
}